package com.onpositive.wiki3.db.impl;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import com.carrotsearch.hppc.IntOpenHashSet;
import com.onpositive.wiki2.whole.model.ITemplateAccess;
import com.onpositive.wiki3.db.catmodel.Categories;
import com.onpositive.wiki3.db.catmodel.Category;
import com.onpositive.wiki3.db.catmodel.matches.classify.RelationConstants;
import com.onpositive.wiki3.db.catmodel.matches.classify.RelationStore;
import com.onpositive.wiki3.db.impl.CountingVocabalary.Pair;

public class Test6 {

	public static void main(String[] args) {
		final WikiDBImpl wikiDBImpl = new WikiDBImpl("C:/work/content");
		try {
			final Categories all = new Categories(wikiDBImpl, new File(
					"C:/work/content/relation.dat"), true);

			RelationStore ss = new RelationStore(wikiDBImpl);

			Categories categories = ss.get(RelationConstants.IS_A);
			ITemplateAccess temlates = wikiDBImpl.getTemlates();

			String string = "aviation";
			// doIt(all, temlates, string, ss);
			// doIt(all, temlates, "atmosphere", ss);
			// /doIt(all, temlates, "life", ss);
			doIt(all, temlates, "aircraft", ss);
			doIt(all, temlates, "tanks", ss);
			doIt(all, temlates, "mammals", ss);
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}

	protected static void doIt(final Categories all, ITemplateAccess temlates,
			String string, RelationStore ss) {
		Category category = all.get(string);
		doit(temlates, category, ss);
	}

	protected static void doit(ITemplateAccess temlates, Category category,
			RelationStore ss) {
		Category[] childCategories = category.getChildCategories();

		Categories categories = ss.get(RelationConstants.IS_A);
		Category category2 = categories.get(category.id());

		CountingVocabalary cc = new CountingVocabalary();
		for (Category c : childCategories) {
			String[] templates = temlates.getTemplates(c.id());
			for (String s : templates) {
				cc.add(s);
			}
		}
		if (category2 != null) {
			IntOpenHashSet mm = new IntOpenHashSet();
			Set<Category> allChildren = category2.getAllChildren();
			for (Category c : allChildren) {
				String[] templates = temlates.getTemplates(c.id());
				for (String s : templates) {
					cc.add(s);
				}
				Category z = c;
				Category category3 = category.getStore().get(z.id());
				int[] children = category3.getChildren();
				for (int q : children) {
					mm.add(q);

				}

			}
			for (int z : mm.toArray()) {
				String[] templates = temlates.getTemplates(z);
				for (String s : templates) {
					cc.add(s);
				}
			}
		}
		boolean f = false;
		System.out.println(category);
		for (Pair p : cc.all()) {
			System.out.println(p);
			f = true;
		}
		if (!f) {
			System.out.println(category + ":" + "empty");
		}
	}

	protected static Category[] doTest(final RelationStore ss,
			Category category, HashMap<Category, Integer> i) {
		Category[] childCategories = category.getChildCategories();

		for (Category z : childCategories) {
			Category[] parentsCategories = z.getParentsCategories();
			HashSet<Category> zz = new HashSet<Category>();
			for (Category m : parentsCategories) {
				Set<Category> all2 = ss.getAll(RelationConstants.IS_A, m);
				zz.addAll(all2);
				// System.out.println(all2);

			}
			for (Category z2 : zz) {
				Integer integer = i.get(z2);
				if (integer == null) {
					integer = 0;
				}

				i.put(z2, integer + 1);
			}
		}
		if (!i.isEmpty()) {
			for (Category m : i.keySet()) {
				if (i.get(m) > childCategories.length / 2) {
					System.out.println(m + ":" + i.get(m));
				}
			}
			System.out.println(i);
		}
		return childCategories;
	}
}
